/**   
 *    
 * @file
 * @brief
 * @文件名	: LoadingCircleListener
 * @版权所有	: 网龙应用软件开发一部
 * @作  者	: chenqian
 * @创建时间	: 2014-7-17 上午10:00:00 
 * @文件描述	:  
 * @version	Ver 1.1
 */
package com.common.widget.ShowBigImage;

import android.graphics.Bitmap;
import android.view.View;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import de.greenrobot.event.EventBus;

/**
 * 加载进度监听 <br>
 * Created 2014-7-17 上午10:00:00
 * 
 * @version 1.2
 * @author chenqian
 * 
 * @see
 */

public class ImageLoaderWithCircleProgressListener implements
		ImageLoadingListener {
	public static final int UNLOAD_CODE = -1;
	public static final int LOAD_SUCCESS_CODE = 100;
	public static final int LOAD_BEGIN_CODE = 0;
	protected LoadImageProgress mProgress;
	protected EventBus mEventBus;
	protected ImageWithLoadingCircle mImageCircle;
	protected String mUrl;

	/**
	 * Creates a new instance of ImageLoaderWithCircleProgressListener. <br>
	 * Created 2014-7-17 下午7:05:54
	 * 
	 * @param url
	 *            原图url
	 * @param imageCircle
	 *            ImageWithLoadingCircle
	 */
	public ImageLoaderWithCircleProgressListener(String url,
			ImageWithLoadingCircle imageCircle) {
		mProgress = new LoadImageProgress(LOAD_BEGIN_CODE, url);
		mEventBus = EventBus.getDefault();
		mImageCircle = imageCircle;
		mUrl = url;
	}

	@Override
	public void onLoadingCancelled(String url, View view) {
		mProgress.percentage = UNLOAD_CODE;
		mEventBus.post(mProgress);
		mEventBus.unregister(this);
	}

	@Override
	public void onLoadingComplete(String url, View view, Bitmap loadedImage) {
		mProgress.percentage = LOAD_SUCCESS_CODE;
		mEventBus.post(mProgress);
		mEventBus.unregister(this);
	}

	@Override
	public void onLoadingFailed(String url, View view, FailReason reason) {
		mProgress.percentage = UNLOAD_CODE;
		mEventBus.post(mProgress);
		mEventBus.unregister(this);
	}

	@Override
	public void onLoadingProgress(long totalSize, long downloadSize) {
		if (0 == totalSize) {
			return;
		}
		mProgress.percentage = (int) (downloadSize * LOAD_SUCCESS_CODE / totalSize);
		mEventBus.post(mProgress);
	}

	@Override
	public void onLoadingStarted(String url, View view) {
		mEventBus.register(this);
		mEventBus.post(mProgress);
	}

	/**
	 * 显示下载进度
	 * 
	 * <br>
	 * Created 2014-7-16 下午5:54:23
	 * 
	 * @param obj
	 *            Object
	 * @author chenqian
	 */
	public void onEventMainThread(Object obj) {
		if (obj instanceof LoadImageProgress) {
			LoadImageProgress progress = (LoadImageProgress) obj;
			if (progress.url.equals(mUrl)) {
				if (progress.percentage == ImageLoaderWithCircleProgressListener.UNLOAD_CODE) {
					mImageCircle.hideCircle();
				} else if (progress.percentage == ImageLoaderWithCircleProgressListener.LOAD_BEGIN_CODE) {
					mImageCircle.showCircle();
				} else if (progress.percentage >= ImageLoaderWithCircleProgressListener.LOAD_SUCCESS_CODE) {
					mImageCircle.hideCircle();
				} else {

					mImageCircle.showProgress(progress.percentage);
				}
			}
		}
	}
}